package com.business.log;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.beans.log.LogTag;
import com.sql.DBFactory;

public class LogTagBus {
	private static final Log log = LogFactory.getLog(LogInfoBus.class);

	private Connection conn = null;

	private Statement stmt = null;

	private PreparedStatement pstmt = null;

	private ResultSet rs = null;

	private String sql = null;

	public void del(String[] tagIds) {

		String delSQL = "delete from log_tag where tag_id=?";
		try {
			conn = DBFactory.getConnection();
			for (int i = 0; i < tagIds.length; i++) {

				stmt = conn.createStatement();
				pstmt = conn.prepareStatement(delSQL);
				pstmt.setString(1, tagIds[i]);
				pstmt.executeUpdate();
				pstmt.close();
			}
		} catch (Exception e) {
			log.error("the delete logtag exception:" + e.getMessage());
			log.error("the delete logtag sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}

	}

	public void update(LogTag lt, String tagId) {
		String tagName = lt.getTagName();
		String viewCount = lt.getViewCount();
		String sql = "update log_tag set tag_name='" + tagName
				+ "',view_count=" + viewCount + " where tag_id=" + tagId;
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
		} catch (Exception e) {
			log.error("the udpate logtag exception:" + e.getMessage());
			log.error("the update logtag sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}


	}
	public LogTag getLogTagById(int tagId){
		LogTag lt=new LogTag();
		String sql = "select * from log_tag where tag_id=" + tagId;
try {
	conn = DBFactory.getConnection();
	stmt = conn.createStatement();
	rs=stmt.executeQuery(sql);
	if(rs.next()){
		lt.setTagId(rs.getString("tag_id"));
		lt.setTagName(rs.getString("tag_name"));
		lt.setViewCount(rs.getString("view_count"));
	}
} catch (Exception e) {
	log.error("the select logtag exception:" + e.getMessage());
	log.error("the select logtag sql is: " + sql);
} finally {
	try {
		rs.close();
		stmt.close();
		conn.close();
	} catch (SQLException sqle) {
		log.error(sqle.toString());
	}
}

	return lt;
	}
}
